//
//  ViewController.m
//  自定义pickView的row
//
//  Created by 伍银荣 on 2021/6/20.
//  Copyright © 2021 com.example. All rights reserved.
//

#import "ViewController.h"
#import "Countries.h"
#import "ModelView.h"

@interface ViewController ()<UIPickerViewDelegate, UIPickerViewDataSource>
@property (strong, nonatomic) IBOutlet UIPickerView *pickView;

@property (nonatomic, strong) NSArray *countriesArr;

@end

@implementation ViewController

-(NSArray *)countriesArr {
    if (!_countriesArr) {
        NSArray *arr = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"contries.plist" ofType:nil]];
        NSMutableArray *mArr = [NSMutableArray arrayWithCapacity:arr.count];
        for (NSDictionary *dict in arr) {
            Countries *model = [Countries countriesWithDictionary:dict];
            [mArr addObject:model];
        }
        _countriesArr = mArr;
    }
    return _countriesArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return self.countriesArr.count;
}

//-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
////    return [self.countriesArr[row] name];
//}

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    ModelView *modelView = [ModelView modelView];
    Countries *model = self.countriesArr[row];
    modelView.countriesModel = model;
    return modelView;
}

-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 100;
}


@end
